package mo.golden;

import mo.common.ListNode;
import mo.common.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class L04_03 {

    public ListNode[] listOfDepth(TreeNode tree) {
        if (tree == null) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        List<ListNode> level = new ArrayList<>();
        queue.add(tree);
        while (!queue.isEmpty()) {
            int size = queue.size();
            ListNode dummy = new ListNode(-1);
            ListNode cur = dummy;
            while (size > 0) {
                TreeNode node = queue.poll();
                cur.next = new ListNode(node.val);
                cur = cur.next;
                size--;
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            level.add(dummy.next);
        }
        return level.toArray(new ListNode[0]);
    }

}
